home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_41.arc / KAYPRO41.ARC / PAIR.C < prev   
Encoding:
C/C++ Source or Header  |  1988-03-15  |  2.5 KB  |  119 lines

  1. /*  Figure from Kaypro Column in Micro Cornucopia Magazine Issue #41
  2. *   PAIR
  3. *
  4. *   Checks whether quotes, )'s, ]'s, etc. are paired in text file
  5. *
  6. *   Written for Small-C compiler vers. 2.03 (ASM)
  7. *
  8. *   Date: 7/8/87
  9. */
  10.  
  11. #include <stdioa.h>
  12. #include "iolib.asm"
  13. #include "call.asm"
  14.  
  15. #define NOCCARGC
  16. #define HIGHBIT 127       /* high bit mask (= 01111111b)   */
  17. #define WORDSTAR          /* check WordStar print controls */
  18.  
  19. int infile,
  20.     lparen,
  21.     rparen,
  22.     lbrack,
  23.     rbrack,
  24.     lcurl,
  25.     rcurl,
  26.     quote,
  27. #ifdef WORDSTAR
  28.     ctrls,
  29.     ctrlb,
  30. #endif
  31.     ok;
  32. char fname[15];
  33.  
  34.  
  35. main(argc,argv) int argc, argv[]; {
  36. int ch;
  37.  
  38.    fputs("PAIR   vers. 2/20/87\n",stderr);
  39.  
  40.    if (argc >= 2)
  41.       infile = fopen(argv[1],"r");
  42.    else {
  43. #ifdef WORDSTAR
  44.       fputs("Checks pairing of ), }, ], \", ^S, and ^B\n",stderr);
  45. #else
  46.       fputs("Checks pairing of ), }, ], and \"\n",stderr);
  47. #endif
  48.       fputs("Name of file to check: ? ",stderr);
  49.       gets(fname);
  50.       infile = fopen(fname,"r");
  51.    }
  52.  
  53.    if (infile == NULL) {
  54.       fputs("File not found.",stderr);
  55.       exit();
  56.    }
  57.  
  58.    lparen = rparen = lbrack = rbrack = 0;
  59.    lcurl  = rcurl  = quote  = ctrls  = ctrlb  = 0;
  60.    ok     = YES;
  61.  
  62.    fputs("\nReading file ...\n",stderr);
  63.  
  64.    while ((ch = getc(infile)) != EOF) {
  65.       switch (ch & HIGHBIT) {            /* strip high bit */
  66.          case ')'    :  ++rparen; break;
  67.          case '('    :  ++lparen; break;
  68.          case ']'    :  ++rbrack; break;
  69.          case '['    :  ++lbrack; break;
  70.          case '}'    :  ++lcurl;  break;
  71.          case '{'    :  ++rcurl;  break;
  72.          case '\"'   :  ++quote;  break;
  73. #ifdef WORDSTAR
  74.          case '\023' :  ++ctrls; break;
  75.          case '\002' :  ++ctrlb; 
  76. #endif
  77.       }
  78.    }
  79.  
  80.    if (lparen != rparen) {
  81.       fputs("Right and left parentheses unequal\n",stderr);
  82.       ok = NO;
  83.    }
  84.  
  85.    if (lbrack != rbrack) {
  86.       fputs("Right and left brackets unequal\n",stderr);
  87.       ok = NO;
  88.    }
  89.  
  90.    if (lcurl != rcurl ) {
  91.       fputs("Right and left curly braces unequal\n",stderr); 
  92.       ok = NO;
  93.    }
  94.  
  95.    if ( quote % 2 != 0 ) {
  96.       fputs("Quotation marks not balanced\n",stderr);
  97.       ok = NO;
  98.    }
  99.  
  100. #ifdef WORDSTAR
  101.  
  102.    if ( ctrls % 2 != 0 ){
  103.       fputs("Ctrl S underline controls not balanced\n",stderr);
  104.       ok = NO;
  105.    }
  106.  
  107.    if ( ctrlb % 2 != 0 ){
  108.       fputs("Ctrl B boldface controls not balanced\n",stderr);
  109.       ok = NO;
  110.    }
  111.  
  112. #endif
  113.  
  114.    if (ok)
  115.       fputs("No errors found\n",stderr);
  116.  
  117.    fclose(infile);
  118. }
  119.